<?php
//购物车
class Cart extends Data_Base
{
	//购物车信息
    public static function detailList ($user)
    {
        $uid = $user->getUserUid();
        if ($uid == 0)
            return array();

        $query = DB::select()
            ->from('app_cart')
            ->where('uid', '=', $uid)
            ->order_by('changed', 'desc')
            ->execute();

        $ret = array();
        foreach ($query as $rs)
        {
            $days  = Util_Date::days($rs['startDay'], $rs['stopDay']);
            $goods = Goods::basicInfo($rs['type'], $rs['goodsId'], $rs['goodsAttrId'], $rs['quantity'], $days);
            $guiderUid = Arr::get($goods, 'uid');
            if (! array_key_exists ($guiderUid, $ret))
            {
                $ret[$guiderUid] = array(
                    'guider' => User::getUserFromUid($guiderUid)->getUserInfo(),
                    'goods' => array(),
                );
            }

            $goods['type']    = $rs['type'];
            $goods['cartId']  = $rs['id'];
            $goods['created'] = $rs['created'];
            $goods['changed'] = $rs['changed'];
            $goods['goodsId'] = $rs['goodsId'];
            $goods['quantity']= $goods['money']['quantity'];
            $ret[$guiderUid]['goods'][] = $goods;
        }

        return $ret;
    }

	//移除购物车
    public static function remove ($user, $arrCartIds)
    {
        if (empty($arrCartIds))
            return FALSE;

        $rows = DB::delete('app_cart')
            ->where('uid', '=', $user->getUserUid())
            ->where('id', 'in', $arrCartIds)
            ->execute();

        return $rows;
    }

	//添加购物车
    public static function add ($uid, $type, $goodsId, $goodsAttrId, $count, $startDay, $stopDay)
    {
        $type  = strtoupper($type);
        $query = DB::select('id')
            ->from('app_cart')
            ->where('uid', '=', $uid)
            ->where('type', '=', $type)
            ->where('goodsId', '=', $goodsId)
            ->where('goodsAttrId', '=', $goodsAttrId)
            ->execute();

        if ($query->count() > 0)
        {
            $current = $query->current();
            $id = $current['id'];
            $query = DB::update('app_cart')
                ->set(array(
                        'quantity' => $count,
                        'startDay' => $startDay,
                        'stopDay'  => $stopDay,
                        'changed' => DB::expr('unix_timestamp(now())')
                    ))
                ->where('id', '=', $id)
                ->execute();
        }
        else
        {
            $row = array(
                'type'    => $type,
                'goodsId' => $goodsId,
                'goodsAttrId' => $goodsAttrId,
                'quantity' => $count,
                'uid'      => $uid,
                'startDay' => $startDay,
                'stopDay'  => $stopDay,
                'created'  => DB::expr('unix_timestamp(now())'),
                'changed'  => DB::expr('unix_timestamp(now())'),
            );

            list($insert_id, $rows) = DB::insert('app_cart')
                ->columns (array_keys($row))
                ->values (array_values($row))
                ->execute();
        }

        return TRUE;
    }
}
